package com.lanqiao.attendance.service.impl;

import com.lanqiao.attendance.mapper.ClassMapper;
import com.lanqiao.attendance.mapper.StudentMapper;
import com.lanqiao.attendance.pojo.Class;
import com.lanqiao.attendance.pojo.Student;
import com.lanqiao.attendance.service.ClassService;
import com.lanqiao.attendance.vo.ClassPartVo;
import com.lanqiao.attendance.vo.ClassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    ClassMapper classMapper;

    @Autowired
    StudentMapper studentMapper;

    /**
     * 管理员插入班级
     *
     * @param cls
     * @return
     */
    @Override
    public int insertClass(Class cls) {
        return classMapper.insertClass(cls);
    }

    /**
     * 管理员通过id查看班级的所有信息
     *
     * @param id
     * @return
     */
    @Override
    public Class queryClassAllById(long id) {
        return classMapper.queryClassAllById(id);
    }

    /**
     * 管理员通过班级名查看班级所有信息
     *
     * @param name
     * @return
     */
    @Override
    public Class queryClassAllByName(String name) {
        return classMapper.queryClassAllByName(name);
    }

    /**
     * 管理员通过班级id删除班级
     *
     * @param id
     * @return
     */
    @Override
    public int deleteClassById(long id) {
        return classMapper.deleteClassById(id);
    }

    /**
     * 学生或老师查看全部班级的部分信息
     *
     * @return
     */
    @Override
    public List<ClassPartVo> queryClassAll() {
        List<Class> classes = classMapper.queryClassAll();
        int i = classes.size();
        if (classes.size()==0){
            return null;
        }
        List<ClassPartVo> classPartVos = new ArrayList<ClassPartVo>();
        for (int j = 0; j < i; j++) {
            List<Student> students = studentMapper.queryStuByClsName(classes.get(j).getName());
            ClassPartVo classPartVo = new ClassPartVo();
            classPartVo.setClassRelNum(students.size());
            classPartVo.setDetail(classes.get(j).getDetail());
            classPartVo.setName(classes.get(j).getName());
            classPartVo.setStartTime(classes.get(j).getStartTime());
            classPartVo.setTeacherName(classes.get(j).getTeacherName());
            classPartVos.add(classPartVo);
        }
        return classPartVos;
    }

    /**
     * 学生或老师根据名称查看班级的部分信息
     *
     * @param name
     * @return
     */
    @Override
    public ClassPartVo queryClassPartByName(String name) {
        Class cls = classMapper.queryClassPartByName(name);
        List<Student> students = studentMapper.queryStuByClsName(name);
        if (cls==null){
            return null;
        }
        int i = students.size();
        ClassPartVo classPartVo = new ClassPartVo();
        classPartVo.setClassRelNum(i);
        classPartVo.setDetail(cls.getDetail());
        classPartVo.setName(cls.getName());
        classPartVo.setStartTime(cls.getStartTime());
        classPartVo.setTeacherName(cls.getTeacherName());
        return classPartVo;
    }

    /**
     * 管理员通过id更新班级信息
     *
     * @param cls
     * @return
     */
    @Override
    public int updateClassAllById(Class cls) {
        return classMapper.updateClassAllById(cls);
    }

    /**
     * 管理员查看全部班级的全部信息
     *
     * @return
     */
    @Override
    public List<Class> queryAllClassAll() {
        return classMapper.queryAllClassAll();
    }

    @Override
    public List<Class> queryByTeacherName(String teaName) {
        return classMapper.queryByTeacherName(teaName);
    }
}
